package com.coolingme.demo.safe;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件夹
 *
 * @author wangyue
 * @date 2020-09-19 17:39
 */
public class Folder extends Directory {

    private List<Directory> items;

    private Integer level;

    public Folder(String name, Integer level) {
        super(name);
        this.level = level;
        items = new ArrayList<>();
    }

    @Override
    public void show() {
        System.out.println(this.name);
        for (Directory directory : this.items) {
            // 文件夹进行循环
            if (this.level != null) {
                for (int i = 0; i < level; i++) {
                    System.out.print("    ");
                }
                for (int i = 0; i < level; i++) {
                    if (i == 0) {
                        System.out.print("+");
                    }
                    System.out.print("----");
                }
            }
            directory.show();
        }
    }

    /**
     * 遍历当前文件夹下的文件或文件夹
     */
    public void list() {
        for (Directory directory : this.items) {
            System.out.println(directory.name);
        }
    }

    /**
     * 添加目录
     *
     * @param directory Directory
     * @return 是否成功
     */
    public boolean add(Directory directory) {
        return this.items.add(directory);
    }

    /**
     * 删除目录
     *
     * @param directory Directory
     * @return 是否成功
     */
    public boolean remove(Directory directory) {
        return this.items.remove(directory);
    }

    /**
     * 根据索引获取目录
     *
     * @param index 索引
     * @return Directory
     */
    public Directory get(Integer index) {
        return this.items.get(index);
    }


}
